package com.hylanda.processors.imagerecognition.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.hylanda.processors.imagerecognition.util.baidu.AuthService;
import com.hylanda.processors.imagerecognition.util.baidu.AuthServiceImageCheck;

public class TestMainImageCheck {

	private static String POST_URL = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/user_defined?access_token="+AuthServiceImageCheck.getAuth();
	public static void main(String[] args) {

		String base64result = Base64ImageUtil.GetUrlImageToBase64(
				//"http://cms-bucket.nosdn.127.net/ee1e8902d95b44bf9558e884297d041320180313232048.jpeg");
				"http://p1.img.cctvpic.com/cportal/img/photoAlbum/page/performance/img/2018/2/18/1518951073412_655_400x300.jpg");

		Base64ImageUtil.GenerateImage(base64result);
		//String base64encode = URLEncoder.encode(base64result);

		String result = "";
		try {
			result = post(base64result);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			

		
		System.out.println(result);
	}
	

	/**
	 * 通过传递参数：url和image进行文字识别
	 *
	 * @param param
	 *            区分是url还是image识别
	 * @return 识别结果
	 * @throws URISyntaxException
	 *             URI打开异常
	 * @throws IOException
	 *             IO流异常
	 */
	private static String post(String param) throws URISyntaxException, IOException {
		// 开始搭建post请求
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost post = new HttpPost();
		URI url = new URI(POST_URL);
		post.setURI(url);
		// 设置请求头，请求头必须为application/x-www-form-urlencoded，因为是传递一个很长的字符串，不能分段发送
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(param, "UTF-8");
		StringEntity entity = new StringEntity(params);
		post.setEntity(entity);
		HttpResponse response = httpClient.execute(post);
		
		 InputStream inStream =     response.getEntity().getContent();  
         BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));  //请注意这里的编码  
         StringBuilder strber = new StringBuilder();    
         String line = null;    
         while ((line = reader.readLine()) != null)     
             strber.append(line );    
         inStream.close();    
         //JSONObject jsonObj = JSONObject.parseObject(strber.toString());
         return strber.toString();
		
		/*System.out.println(response.toString());
		if (response.getStatusLine().getStatusCode() == 200) {
			String str;
			try {
				 读取服务器返回过来的json字符串数据 
				str = EntityUtils.toString(response.getEntity());
				System.out.println(str);
				return str;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;*/
	}
}
